package edu.jhu.secondhome.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.jhu.secondhome.database.beans.Hotel;

import edu.jhu.secondhome.database.accessors.HotelAccessor;
import edu.jhu.secondhome.database.accessors.RoomAccessor;
import edu.jhu.secondhome.database.beans.Room;

public class AvailableRoomsServlet extends HttpServlet
{  
  /**
   * 
   **/
  private static final long serialVersionUID = 1148817190309774021L;

  private static final long MS_PER_DAY = 86400000;

  public void init() throws ServletException
  {

  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
    processRequest(req, resp);
      }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
    processRequest(req, resp);
      }

  public void processRequest(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException
      {
    response.setContentType("text/html;charset=UTF-8");
    ServletContext servletContext = getServletContext();
    HttpSession session = request.getSession();

    ArrayList<String> errors = new ArrayList<String>();
    request.setAttribute("errors", errors);

    String hotelName = request.getParameter( "hotelName" );
    if ( hotelName == null || hotelName.equals( "" ) )
    {
      hotelName = (String) request.getAttribute( "hotelName" );
    }
    
    String checkin = request.getParameter( "check-in" );
    if ( checkin == null || checkin.equals( "" ) )
    {
      checkin = (String) request.getAttribute( "check-in" );
    }
    
    String numDays = request.getParameter( "numDays" );
    if ( numDays == null || numDays.equals( "" ) )
    {
      numDays = (String) request.getAttribute( "numDays" );
    }
    
    String numPeople = request.getParameter( "numPeople" );
    if ( numPeople == null || numPeople.equals( "" ) )
    {
      numPeople = (String) request.getAttribute( "numPeople" );
    }

    try
    {
      int occupancy = Integer.parseInt( numPeople );
      int duration = Integer.parseInt( numDays );
      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      java.util.Date utilCheckinDate = format.parse( checkin );

      if( !( utilCheckinDate instanceof java.util.Date ) )  
      {
        errors.add( "You must provide check-in and check-out dates in the form MM/dd/yyyy." );
        RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);
        return;
      }

      Date checkinDate = new Date( utilCheckinDate.getTime() );
      RoomAccessor roomsDb = new RoomAccessor();
      HotelAccessor hotelsDb = new HotelAccessor();
      
      long start = checkinDate.getTime();
      long end = start + (MS_PER_DAY * duration);
      String checkout = format.format( new Date( end ) );

      List<Room> rooms = roomsDb.getRoomsForHotelWithCapacityFreeOnDate( hotelName, occupancy, checkinDate, duration );
      Hotel hotel = hotelsDb.getHotelByName( hotelName );

      request.setAttribute( "hotel", hotel );
      request.setAttribute( "rooms", rooms );
      request.setAttribute( "check-in", checkin );
      request.setAttribute( "check-out", checkout );
      request.setAttribute( "numDays", duration );
      request.setAttribute( "numPeople", occupancy );

      RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/search/hotelInfo.jsp");
      dispatcher.forward(request, response);
      return;
    } catch ( SQLException e1 )
    {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      errors.add( e1.getMessage() );
    } catch ( ParseException pe )
    {
      errors.add( "You must supply valid check-in and check-out dates." );
    } catch ( NumberFormatException nfe )
    {
      errors.add( "You must supply a valid integer for number of people." );
    }

    RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
    return;
      }
}
